﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace cnFederal
{
    public class CnfiAds
    {
        private static CnfiDataDataContext lnq;
        private static List<CnfiAd> _ads;

        public CnfiAds(){}
        public List<CnfiAd> Ads
        {
            get
            {
                return _ads;
            }
            set
            {
                _ads = value;
            }
        }


        public static void Load(bool display = true)
        {
            lnq = new CnfiDataDataContext();
            _ads = new List<CnfiAd>();
            if (_ads != null)
            {
                List<CnfiAd> cAds = new List<CnfiAd>();
                var b = from c in lnq.Ads
                        where c.Display == display
                        select c;

                foreach (var s in b)
                {
                    CnfiAd cAd = new CnfiAd();
                    cAd.Id = (int)s.Id;
                    cAd.Title = (string)s.Title;
                    cAd.Description = (string)s.Description;
                    cAd.ImageUrl = (string)s.ImageUrl;
                    cAd.AdsUrl = (string)s.AdsUrl;
                    cAd.Display = (bool)s.Display;
                    cAd.DateRegistered = (DateTime)s.DateRegistered;
                    cAd.DateStartPublish = (DateTime)s.DateStratPublish;
                    cAd.DateEndPublish = (DateTime)s.DateEndPublish;


                    cAds.Add(cAd);

                }

                _ads = cAds;
            }
        }

        public void RefreshPostlist()
        {
            _ads.Clear();
            _ads = GetAllFaPosts();
        }
        public static List<CnfiAd> GetAllPostsFromDateToDate()
        {
            List<CnfiAd> liste = new List<CnfiAd>();
            if (_ads != null)
            {
                foreach (CnfiAd post in _ads)
                {
                    if (post.DateStartPublish <= DateTime.Now &&
                       post.DateEndPublish >= DateTime.Now)
                        liste.Add(post);
                }

            }

            return Enumerable.Reverse(liste).ToList();
        }
        public static CnfiAd GetTheLasAd()
        {
            List<CnfiAd> ls = GetAllPostsFromDateToDate();
            CnfiAd post = null;
            if (ls == null || ls.Count == 0) return null;
            else
                post = ls.First();
            return post;
        }
        public static List<CnfiAd> GetAllFaPosts()
        {

            return Enumerable.Reverse(_ads).ToList();
        }

        public static List<CnfiAd> GetTopPosts(int number)
        {
            if (_ads == null) return null;
            return _ads.Take(number).ToList();
        }

        public static CnfiAd GetFaPostById(int id)
        {
            if (_ads == null) return null;
            CnfiAd cAd = null;
            var b = from c in _ads
                    where c.Id == id
                    select c;
            foreach (var s in b)
            {
                cAd = new CnfiAd();
                cAd.Id = (int)s.Id;
                cAd.Title = (string)s.Title;
                cAd.Description = (string)s.Description;
                cAd.Display = (bool)s.Display;
                cAd.AdsUrl = (string)s.AdsUrl;
                cAd.ImageUrl = (string)s.ImageUrl;
                cAd.DateStartPublish = (DateTime)s.DateStartPublish;
                cAd.DateRegistered = (DateTime)s.DateRegistered;
                cAd.DateEndPublish = (DateTime)s.DateEndPublish;

            }

            return cAd;
        }


        public static int CreateNew(CnfiAd cAd)
        {
            int lastId = 0;
            try
            {
                Ad lk = new Ad();
                lk.Title = cAd.Title;
                lk.Description = cAd.Description;
                lk.AdsUrl = cAd.AdsUrl;
                lk.ImageUrl = cAd.ImageUrl;
                lk.Display = cAd.Display;
                lk.DateStratPublish = cAd.DateStartPublish;
                lk.DateEndPublish = cAd.DateEndPublish;
                lk.DateRegistered = cAd.DateRegistered;
                lnq.Ads.InsertOnSubmit(lk);
                lnq.SubmitChanges();
                lnq.Connection.Close();

                lastId = (int)(from c in lnq.Links
                               select c.Id).Max();
            }
            catch (Exception ex)
            {
               
            }
            return lastId;
        }

        public static bool DeleteItem(int id)
        {
            bool deleted = false;
            var b = from f in lnq.Ads
                    where f.Id == id
                    select f;
            if (b != null && b.Count() > 0)
            {
                Ad n = b.First();
                try
                {
                    lnq.Ads.DeleteOnSubmit(n);
                    lnq.SubmitChanges();
                    deleted = true;
                }
                catch (Exception ex)
                {
                    deleted = false;
                }
            }
            return deleted;

        }

        public static void Save(CnfiAd cAd)
        {
            var b = from f in lnq.Ads
                    where f.Id == cAd.Id
                    select f;
            foreach (var ad in b)
            {
                ad.Title = cAd.Title;
                ad.Description = cAd.Description;
                ad.AdsUrl = cAd.AdsUrl;
                ad.ImageUrl = cAd.ImageUrl;
                ad.Display = cAd.Display;
                ad.DateStratPublish = cAd.DateStartPublish;
                ad.DateEndPublish = cAd.DateEndPublish;
                ad.DateRegistered = cAd.DateRegistered;
            }
            lnq.SubmitChanges();
        }



    }


    public class CnfiAd
    {
        public int Id
        {
            get;
            set;
        }

        public string Title
        {
            get;
            set;
        }
        public string Description
        {
            get;
            set;
        }
        public string ImageUrl
        {
            get;
            set;
        }
        public string AdsUrl
        {
            get;
            set;
        }
        public bool Display
        {
            get;
            set;
        }
        public DateTime DateRegistered
        {
            get;
            set;
        }

        public DateTime DateStartPublish
        {
            get;
            set;
        }

        public DateTime DateEndPublish
        {
            get;
            set;
        }

    }
}